//
//  ByViewOptionalClassScroll.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/14.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewOptionalClassScroll.h"
#define SELECT_COLOR [UIColor colorWithR:207 g:231 b:130]

@implementation ByCellOptionalClassificationItem
{
    UILabel     *_titleLabel;
    UIImageView *_arrowImg;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        self.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    }
    return self;
}
- (void)setupUI {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = FONT(14);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    CGFloat width = [@"神经科科科科" getSizeOfString:14].width + 30;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    _arrowImg = [[UIImageView alloc] init];
    [self addSubview:_arrowImg];
    [_arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    _arrowImg.backgroundColor = BY_THEME_COLOR;
    _arrowImg.hidden = YES;
    
}

-(void)setCateModel:(ByModelPhysicalOptional *)cateModel{
    _cateModel = cateModel;
    _titleLabel.text = _cateModel.name;
}

- (void)setSelecting:(BOOL)selecting {
    
    _selecting = selecting;
    self.backgroundColor = _selecting ? [UIColor whiteColor]:[UIColor colorWithWhite:0.90 alpha:1];
    _titleLabel.textColor = _selecting ? BY_THEME_COLOR : [UIColor blackColor];
    _arrowImg.hidden = !_selecting;
    if (_selecting) {
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_arrowImg.mas_right).offset(10);
            make.top.equalTo(@(10));
            make.right.equalTo(@(-10));
            make.bottom.equalTo(@(-10));
        }];
    }else{
        CGFloat width = [@"神经科科科科" getSizeOfString:14].width + 30;
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(10));
            make.top.equalTo(@(10));
            make.size.mas_equalTo(CGSizeMake(width, 20));
        }];
    }
}

@end
@implementation ByViewOptionalClassScroll

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    _selectingIndex = 0;
    
    self.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    
}



#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.classModels.count;;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellOptionalClassificationItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellOptionalClassificationItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.selecting = (indexPath.row == _selectingIndex);
    
    ByModelPhysicalOptional *model = self.classModels[indexPath.row];
    cell.cateModel = model;
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击当前的
    if (_selectingIndex == indexPath.row) {
        return;
    }
    
    _selectingIndex = indexPath.row;
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    [self reloadData];
    
    if (self.selectBlock) {
        self.selectBlock(self.classModels[indexPath.row]);
    }
}



@end
